import React, { useRef } from 'react';
import useForm from './useForm';
import FormContext from './formContext';

const Form = ({ onFinish, initialValue, children }) => {
    const [formInstance] = useForm();
    const mountRef = useRef();
    formInstance.setCallbacks({
        onFinish,
    });
    // if (!mountRef.current) {
    //     formInstance.setInitialValue(initialValue);
    //     mountRef.current = true;
    // }

    return (
        <form
            onSubmit={(event) => {
                event.preventDefault();
                event.stopPropagation();
                // 这个submit就是我们写的那个submit，只是包装了一层，把form收集到的values传给我们了
                formInstance.submit();
            }}
        >
            <FormContext.Provider value={formInstance}>
                {children}
            </FormContext.Provider>
        </form>
    );
};

export default Form;
